package es.udc.pfc.davidparedes.model.store;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.hibernate.annotations.Where;

import es.udc.pfc.davidparedes.model.universe.Universe;

@Entity
public class Store {
	
	private long storeId;
	private String name;
	private String address;
	private Calendar deletionDate;
	private long version;
	
	private Set<Universe> universes = new HashSet<Universe>();
	
	public Store() {
	}
	
	public Store(String name, String address) {
		this.name = name;
		this.address = address;
	}
	
	@Column (name = "id")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getStoreId() {
		return storeId;
	}
	
	public void setStoreId(long storeId) {
		this.storeId = storeId;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getDeletionDate() {
		return deletionDate;
	}
	
	public void setDeletionDate(Calendar deletionDate) {
		this.deletionDate = deletionDate;
	}
	
	@Version
	public long getVersion() {
		return version;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}
	
	@OneToMany(mappedBy = "store")
	@Where(clause="deletionDate is null")
    public Set<Universe> getUniverses() {
        return universes;
    }
	
    public void setUniverses(Set<Universe> universes) {
    	this.universes = universes;
    }
    
    public void addUniverse(Universe universe) {
    	universes.add(universe);
    	universe.setStore(this);
    }
    
    public void removeUniverse(Universe universe) {
    	universes.remove(universe);
    }
	
	@Override
	public String toString() {
		return ("Store [storeId=" + storeId + ", name=" + name
				+ ", address=" + address + ", version=" + version + "]");
	}

}
